﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ThoiKhoaBieuProvider
    {
        public static List<ThoiKhoaBieu> GetThoiKhoaBieu()
        {
            List<ThoiKhoaBieu> lst = new List<ThoiKhoaBieu>();
            using (QLTTTHEntities context=new QLTTTHEntities())
            {
                var query = from tkb in context.ThoiKhoaBieux select tkb;
                foreach (var item in query)
                {
                    ThoiKhoaBieu tk = new ThoiKhoaBieu();
                    tk.MaTKB = item.MaTKB;
                    tk.MaPhong = item.MaPhong;
                    tk.MaLop = item.MaLop;
                    tk.NgayHoc = item.NgayHoc;
                    tk.MaLop = item.MaLop;
                    tk.BuoiHoc = item.BuoiHoc;
                    lst.Add(tk);
                }
            }
            return lst;
        }

        #region-------------------Xóa thời khóa biểu ------------------------
        public static bool XoaThoiKhoaBieu(int maTKB)
        {

            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var tkb = context.ThoiKhoaBieux.Single(q => q.MaTKB.Equals(maTKB));
                    context.ThoiKhoaBieux.Remove(tkb);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        public static int ThemThoiKhoaBieu(ThoiKhoaBieu tkb)
        {
            int maTKB = -1;
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    context.ThoiKhoaBieux.Add(tkb);
                    context.SaveChanges();
                    maTKB = tkb.MaTKB;
                }
            }
            catch
            {

            }
            return maTKB;
        }


        public static bool CapNhatThoiKhoaBieu(ThoiKhoaBieu tkb)
        {
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var query = (from u in context.ThoiKhoaBieux where u.MaTKB.Equals(tkb.MaTKB) select u).First();
                    query.MaLop = tkb.MaLop;
                    query.NgayHoc = tkb.NgayHoc;
                    query.BuoiHoc = tkb.BuoiHoc;
                    query.MaPhong = tkb.MaPhong;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
